<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2016/11/23
 * Time: 10:31
 */
require("Include/conn.php");
global $link;
if($_COOKIE['user_id']!="")
{
    $user_id = $_COOKIE['user_id'];
}
if ($_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/png")
{
    if ($_FILES["file"]["error"] > 0)
    {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
    else
    {
        if (file_exists("upload/" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " Already Exists. ";
        }
        else
        {
            require("Include/conn.php");
            $user_id = $_COOKIE['user_id'];
            $pic_name=$_POST['pic_name'];
            $pic_instr=$_POST['pic_instr'];
            $sql = "INSERT INTO `pw_pic` (`pic_name`,`pic_userid`,`pic_time`,`pic_praisecnt`,`pic_favcnt`,`pic_stat`,`pic_instr`) VALUES('$pic_name',$user_id,now(),0,0,1,'$pic_instr');";    //注意分号
            $result = mysqli_query($link,$sql);

            if ($result) echo '<script>alert("作品发布成功！");window.location.href="user.php";</script>';
            else    echo '<script>alert("作品发布失败！");history.go(-2);</script>';//

            $sql = "SELECT LAST_INSERT_ID()";
            $result = mysqli_query($link, $sql);
            $row = mysqli_fetch_array($result);
            $picid = $row[0];

            move_uploaded_file($_FILES["file"]["tmp_name"], "img/upload/" . $picid . ".jpg");
            //imagepng(imagecreatefromstring(file_get_contents( "img/upload/" . $picid . ".jpg")), "img/upload/" . "$picid.png");
        }
    }
}
else
{
    echo '<script>alert("照片分辨率过高！");history.go(-2);</script>';
}
?>